// subPackages3//pages/gameManage/gameList/gameList.js
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
import Serv from './gameListSer.js';
import formatDate from '../../../../utils/util2.js'
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkUserInfo: {},//判断当前用户是否可以抽奖
    //0中奖，1助力
    currentTab: 0,
    gameActivityId:'',
    gamePrizeList:[],
    assistanceList:[],
    showModel:false,
    showMask:false,
    userInfo:null,
    userData:null,
    // prizeDetail:null,
    clientHeight:'',
    showHelp:false,
    gameHighSetData:{},
    swiper:{
      prizeDetail:[],
      current:0
    },
    prizeParmas:{
      pageNo:1,
      pageSize:15,
      total:null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    if(options.success){
      wx.showToast({
        title:'领取地址新增成功！',
        icon:'success'
      })
    }
    let that = this;
    wx.getSystemInfo({ 
      success: function (res) { 
          that.setData({ 
              clientHeight: res.windowHeight 
          }); 
      }
    }) 
    console.log(options.type)
    this.setData({
      currentTab:options.type,
      // title:options.title,
    })
    let userInfo = wx.getStorageSync('userInfo')
    let userData = wx.getStorageSync('userData')
    that.setData({
      userInfo:wx.getStorageSync('userInfo'),
      userData:wx.getStorageSync('userData'),
      gameActivityId:options.gameActivityId||wx.getStorageSync('gameInfo').gameActivityId
    })
    // debugger
    let params = {
      // gameActivityId:'2c94817270d28b520170d28b521e0000',
      gameActivityId:options.gameActivityId||wx.getStorageSync('gameInfo').gameActivityId,
      mobilePhone:userData.mobile
    }
    // this.showMyPrize()
    this.getGamePrizeList()
    this.getGameUserList(params)
    this.getHighSetData();
    this.getGameActivityDetail();
    this.inviteF();
    if(options.type==0&&options.isShow==1){
      this.showMyPrize()
    }
    this.checkUser();
  },

  swichNav: function( e ) {

    var that = this;

    if( that.data.currentTab === e.target.dataset.current ) {
        return false;
    } else {
        that.setData( {
            currentTab: e.target.dataset.current
        })
    }
  },
  bindChange(e){
    var that = this;
    // debugger
    if( that.data.currentTab === e.detail.current ) {
      return false;
  } else {
      that.setData( {
          currentTab: e.detail.current
      })
  }
  },
  async getGamePrizeList(payload){
    let { prizeParmas, gamePrizeList} = this.data
    let { pageNo, pageSize} = prizeParmas
    let params = { gameActivityId: this.data.gameActivityId, pageNo, pageSize, ...payload}
    let {success,result,message} = await Serv.getGamePrizeList(params)
    var reg=/(\d{3})\d{4}(\d{4})/;
   
    if(success){
      result.records.forEach(item=>{
        item.createTime = item.createTime.split(' ')[0]
        item.createTime=item.createTime.replace(/-/g,'.')
        item.mobilePhone = item.mobilePhone.replace(reg, "$1****$2")
      })
      prizeParmas.pageNo=result.current
      prizeParmas.total = result.total
      gamePrizeList = [...gamePrizeList, ...result.records]
      this.setData({
        gamePrizeList, 
        prizeParmas
      })
    }else{
      wx.showToast({
        title:message,
        icon:'none'
      })
    }
    
  },
  async getGameUserList(params){
    let {success,result,message} = await Serv.getGameUserList(params)
    // debugger
    if(success){
      this.setData({
        assistanceList:result.assistanceList||[]
      })
    }else{
      wx.showToast({
        title:message,
        icon:'none'
      })
    }
    
    // this.data.assistanceList = dataList.result.assistanceList||[]
  },
  async inviteF(){
    // let userInfo = wx.getStorageSync('userInfo')
    // let userData = wx.getStorageSync('userData')
    let pamars = {
      nickname:this.data.userInfo.nickName,
      mobilePhone:this.data.userData.mobile,
      gameActivityId:this.data.gameActivityId
    }
    
    let {success,message} = await Serv.inviteCountServ(pamars)
    if(success){
    }else{
      wx.showToast({
        title:message,
        icon:'none'
      })
    }
    this.setData({
      isCheckActivityUser:success
    })
    wx.showShareMenu();
  },
  async showMyPrize(){
    let pamars = {
      gameActivityId:this.data.gameActivityId||wx.getStorageSync('gameInfo').gameActivityId,
      mobilePhone:this.data.userData.mobile
    }
    // if(this.data.showModel){
    //   this.setData({
    //     showModel:false
    //   })
    //   return
    // }
    let {success,result,message} = await Serv.userPrizeDetail(pamars)
    // result.prizeFlag = 1//test数值
    if(success){
      result.forEach(item => {
        if(item.name==null){
          item.name = ''
        }
        if(item.mobile==null){
          item.mobile = ''
        }
        if(item.detailAddress==null){
          item.detailAddress = ''
        }
        if(item.redeemRemark==null){
          item.redeemRemark = ''
        }
      });
      this.data.swiper.prizeDetail = result
      console.log(this.data.swiper.prizeDetail);
      this.setData({
        swiper: this.data.swiper,
        showModel:!this.data.showModel,
        showMask:!this.data.showModel
      })
    }else{
      wx.showToast({
        title:message,
        icon:'none'
      })
    }
    // debugger
  },

  async getGameActivityDetail(){
    let pamars = {
      gameActivityId:this.data.gameActivityId||wx.getStorageSync('gameInfo').gameActivityId,
    }
    let {success,result} = await Serv.getGameActivityDetail(pamars)
    // debugger
    if(success){
      this.setData({
        title:result.title
      })
    }
  },
  closeModel(){
    this.setData({
      showModel:!this.data.showModel,
      showMask:!this.data.showModel
    })
  },
  prevImg(){
    var swiper = this.data.swiper;
    var current = swiper.current;
    swiper.current = current > 0 ? current - 1 : swiper.prizeDetail.length - 1;
    this.setData({
      swiper: swiper,
    })
  },
  nextImg(){
    var swiper = this.data.swiper;
    var current = swiper.current;
    swiper.current = current < (swiper.prizeDetail.length - 1) ? current + 1 : 0;
    this.setData({
      swiper: swiper,
    })
  },
  gotoAddress(e){
    let index =  e.currentTarget.dataset.index
    let { detailAddress,mobile,name,prizeId } = this.data.swiper.prizeDetail[index]
    let isRevise = false;
    if(detailAddress&&mobile&&name){
      isRevise = true;
    }
    wx.navigateTo({
      url: `/subPackages3/pages/gameManage/addressMaintenance/addressMaintenance?detailAddress=${detailAddress}&mobile=${mobile}&name=${name}&isRevise=${isRevise}&prizeId=${prizeId}`
    })

  },
  //跳转到兑奖楼盘列表页面
  gotoTakePrizeAddress(e){
    let index = e.currentTarget.dataset.index
    let prizeDetail = this.data.swiper.prizeDetail[index];
    wx.navigateTo({
      url: `/subPackages3/pages/gameManage/gameTakePrizeAddress/gameTakePrizeAddress?customerPhone=${prizeDetail.mobile}&gameActivityAwardId=${prizeDetail.gameActivityAwardId}`
    })
  },
  gotoTakePrizeCode(e){
    let index = e.currentTarget.dataset.index
    let prizeDetail = this.data.swiper.prizeDetail[index];
      wx.navigateTo({
        url: `/subPackages3/pages/gameManage/gameTakePrizeCode/gameTakePrizeCode?objectId=${prizeDetail.prizeId}&actionType=${prizeDetail.awardType}`
      })
  },
  userNoGame() {
    let checkUserInfo = this.data.checkUserInfo;
    if (!checkUserInfo.checkUserStatus) {
      wx.showToast({
        title: checkUserInfo.message,
        icon: 'none'
      });
    }
  },
  showHelp(){
    this.setData({
      showMask:!this.data.showHelp,
      showHelp:!this.data.showHelp,
      showModel: false
    })
  },
  showHelp2() {
    this.setData({
      showMask: false,
      showHelp: false
    })
  },
  //预加载海报图片
  preload: function () {
    let { gameHighSetData } = this.data;
    var userInfoData = wx.getStorageSync('userInfoData')
    var userInfo = wx.getStorageSync('userInfo')
    let bg = [`${(gameHighSetData && gameHighSetData.posterImgUrl) ? gameHighSetData.posterImgUrl : ''}`, userInfoData.headImgUrl || userInfo.avatarUrl];
    // //debugger
    formatDate.imgPreLoad(bg, (preLoadImg) => {
      wx.setStorageSync('preLoadImg', preLoadImg)
    })
  },
  //获取高级设置的数据
  async getHighSetData() {
    let that = this;
    let params = {
      gameActivityId: that.data.gameActivityId
    };
    let { success, result } = await Serv.getHighSetData(params);
    console.log(result);
    if (success) {
      if (result) {
        if(result.allowSharing==0){
          wx.hideShareMenu()
        }
        that.setData({
          gameHighSetData: result,
          assistedOpening:result.assistedOpening
        });
        that.preload();
      }
    }
  },
  // 分享海报
  toPoster(){
    let gameHighSetData = this.data.gameHighSetData;
    console.log("生成助力海报");
    // wx.navigateTo({
    //   url: `/subPackages/pages/housePoster/housePoster?gameActivityId=${this.data.gameActivityId}&type=4&pageName=gameIndex&posterPic=${(gameHighSetData && gameHighSetData.posterImgUrl && gameHighSetData.posterImgUrl != '') ? gameHighSetData.posterImgUrl:''}`
    // })
    wx.navigateTo({
      url: `/subPackages/pages/housePoster/housePoster?gameActivityId=${this.data.gameActivityId}&type=4&actionType=${gameHighSetData.assistedOpening == 1?'help':''}&pageName=gameIndex&posterPic=${(gameHighSetData && gameHighSetData.posterImgUrl && gameHighSetData.posterImgUrl != '') ? gameHighSetData.posterImgUrl:''}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!app.globalData.isAddUserSuccess){
      app.addUser(this.data.gameActivityId)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  pullDownRefresh(){
    let { prizeParmas, gamePrizeList} = this.data
    prizeParmas.pageNo += 1
    this.setData({
      prizeParmas
    })
    if (gamePrizeList.length<prizeParmas.total){
      this.getGamePrizeList(prizeParmas)
    }
  },
  async checkUser() {
    let userData = wx.getStorageSync("userData");
    let params = {
      gameActivityId: this.data.gameActivityId,
      mobilePhone: userData.mobile
    }
    let { success, result, message } = await Serv.checkUserServ(params);
    let checkUserInfo = {
      checkUserStatus: success,
      message: message
    };
    this.setData({
      checkUserInfo: checkUserInfo
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _this =this
    let userInfo = wx.getStorageSync('userInfo')
    let userData = wx.getStorageSync('userData')
    let {gameHighSetData} = this.data
    let helpPamars = {
      nickname:userInfo.nickName,
      mobilePhone:userData.mobile,
      gameActivityId:this.data.gameActivityId
    }
    let shareObj = {
      title: gameHighSetData.sharingTitle,
      imageUrl: gameHighSetData.sharingImgUrl
    }
    helpPamars = JSON.stringify(helpPamars)
    if (app.globalData.isAddUserSuccess) {
      if(gameHighSetData.allowSharing==1&&_this.data.isCheckActivityUser==false){  //可分享但不是业主
        shareObj.path = `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?gameActivityId=${this.data.gameActivityId}`
      }
      else if(gameHighSetData.allowSharing==1&&gameHighSetData.assistedOpening==0){//可分享不可助力，分享当前活动而不是助力
        shareObj.path = `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?gameActivityId=${this.data.gameActivityId}`
        // shareObj = {
        //   title: gameHighSetData.sharingTitle,
        //   path: `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?gameActivityId=${this.data.gameActivityId}`,
        //   imageUrl: gameHighSetData.sharingImgUrl
        // }
      }else{
        shareObj.path = `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?helpPamars=${helpPamars}&actionType=help`
  
        // shareObj = {
        //   title: gameHighSetData.sharingTitle,
        //   path: `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?helpPamars=${helpPamars}&actionType=help`,
        //   imageUrl: gameHighSetData.sharingImgUrl
        // }
      }
      //  wx.navigateTo({
      //     url:`/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?gameActivityId=${this.data.gameActivityId}`
      //   })
      // debugger
        // wx.navigateTo({
        //   url:`/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?helpPamars=${helpPamars}&actionType=help`
        // })
        _this.setData({
          showMask:false,
          showHelp:false
        })
        console.log(shareObj)
      // return {
      //   title: '邀请好友助力',
      //   path: `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?helpPamars=${helpPamars}&actionType=help`,
      //   imageUrl: this.data.activityDetail.titleImgUrl
      // }
      }else{
        shareObj.path = `/subPackages3/pages/gameManage/gameTransferStation/gameTransferStation?gameActivityId=${this.data.gameActivityId}`
      }
      return shareObj
  }
})